import lightgbm as lgb  # 添加LightGBM导入
import pandas as pd
from sklearn.metrics import accuracy_score, roc_auc_score, classification_report, precision_score, recall_score, f1_score
import numpy as np
# 加载预处理后的数据
data_path = '/data/GuoCu_data/processed_data/toGBDT.csv'
df = pd.read_csv(data_path)

# 分离特征和标签
X = df.drop('label', axis=1)
y = df['label'].values  # 直接转换为NumPy数组

# 设置GBDT模型参数
params = {
    'objective': 'binary',
    'metric': 'auc',
    'boosting_type': 'gbdt',
    'num_leaves': 63,
    'max_depth': 6,
    'learning_rate': 0.001,
    'feature_fraction': 1,
    'bagging_fraction': 0.7,
    'bagging_freq': 5,
    'verbose': 0,
    'scale_pos_weight': 9.0,
    'boost_from_average': True,
    'lambda_l1': 0.01,
    'lambda_l2': 0.01,
    'min_data_in_leaf': 5,
    'min_sum_hessian_in_leaf': 1
}

# 决策阈值
threshold = 0.3

print("使用全部数据训练 GBDT 模型...")

# 创建 LightGBM 数据集
lgb_train = lgb.Dataset(X, y)

# 训练模型
model = lgb.train(
    params,
    lgb_train,
    num_boost_round=2000,
    valid_sets=[lgb_train],
    callbacks=[
        lgb.early_stopping(stopping_rounds=100),
        lgb.log_evaluation(period=100)
    ]
)

# 模型预测（仅用于评估）
y_pred = model.predict(X, num_iteration=model.best_iteration)
y_pred_binary = np.array([1 if x >= threshold else 0 for x in y_pred])

# 模型评估
auc = roc_auc_score(y, y_pred)
accuracy = accuracy_score(y, y_pred_binary)
precision = precision_score(y, y_pred_binary)
recall = recall_score(y, y_pred_binary)
f1 = f1_score(y, y_pred_binary)

print("\n===== 模型在训练集上的评估结果 =====")
print(f"AUC: {auc:.4f}")
print(f"准确率: {accuracy:.4f}")
print(f"精确率: {precision:.4f}")
print(f"召回率: {recall:.4f}")
print(f"F1 分数: {f1:.4f}")
print("分类报告：")
print(classification_report(y, y_pred_binary))

# 保存模型
model_output_path = '/data/GuoCu_data/models/final_lgb_model.txt'
model.save_model(model_output_path)
print(f"\n✅ 模型已保存至: {model_output_path}")
